package com.hq.zb.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hq.zb.dto.DeptDto;
import com.hq.zb.dto.OrgDto;
import com.hq.zb.entity.CurrentUser;
import com.hq.zb.model.Dept;
import com.hq.zb.service.IDeptService;
import com.hq.zb.utils.ContextHolderUtils;
import com.hq.zb.utils.Result;
import com.hq.zb.vo.DeptVo;
import com.hq.zb.vo.SelectCommVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门controller.
 *
 * @author huanghanrui
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/sys")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    /**
     * 分页查询部门
     * @param deptDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/dept/query")
    @ResponseBody
    public Result queryDepts(@RequestBody DeptDto deptDto, int pageNum, int pageSize) {
        Wrapper<DeptVo> wrapper = new EntityWrapper<>();
        wrapper.eq("t.delete_flag", false);
        wrapper.like("t.dept_code", deptDto.getDeptCode());
        wrapper.like("t.dept_name", deptDto.getDeptName());
        wrapper.like("t.parent_dept", deptDto.getParentDept());
        wrapper.like("t.org_code", deptDto.getOrgCode());
        Page<DeptVo> page = new Page<>(pageNum, pageSize);
        Page<DeptVo> list = deptService.listByPage(page, wrapper);
        Result.ok();
        return Result.data(list);
    }

    /**
     * 保存组织
     * @param deptDto
     * @return
     */
    @PostMapping("/dept/save")
    public Result saveDept(@RequestBody DeptDto deptDto) {
        deptService.saveDept(deptDto);
        return Result.ok();
    }

    /**
     * 删除部门
     * @param depts
     * @return
     */
    @PostMapping("/dept/delete")
    public Result deleteDept(@RequestBody List<Dept> depts) {
        deptService.deleteDept(depts);
        return Result.ok();
    }

    /**
     * 查询
     * @param deptDto 参数
     * @return
     */
    @PostMapping("/dept/queryList")
    @ResponseBody
    public Result queryDept(@RequestBody DeptDto deptDto) {
        //获取当前登陆用户信息
        CurrentUser currentUser = ContextHolderUtils.getCurrentUser();
        List<SelectCommVo> deptVos = deptService.queryList(deptDto, currentUser);
        String data = JSONObject.toJSONString(deptVos, SerializerFeature.NotWriteDefaultValue);
        return Result.data(JSONObject.parse(data));
    }
}

